//
//  PLMacro.h
//  PlatformCommon
//
//  Created by KimmanYip on 2019/3/22.
//  Copyright © 2019 KimmanYip. All rights reserved.
//

/*
 * 常用宏定义
 */

#ifndef PLMacro_h
#define PLMacro_h

#pragma mark - 运行环境

#ifndef IS_RELEASE

#ifndef __OPTIMIZE__
// 非release
#define IS_RELEASE NO
#else
// release环境
#define IS_RELEASE YES
#endif

#endif

#pragma mark - 打印模式
/* 在debug模式下打印,release不打印 */
/* 定义在 PROJECT/Target > Build Settings > Preprocessor Macros > DEBUG=1 */
#if DEBUG
#define NSLog(format, ...) printf("\nNSLog %s(line%d) %s\n%s\n\n", [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, __PRETTY_FUNCTION__, [[NSString stringWithFormat:(format), ##__VA_ARGS__] UTF8String])
#else
#define NSLog(format, ...)
#endif

#pragma mark - 设备判断

// 型号
// iPhone4S
#define iPhone4S ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)

// iPhone5 iPhone5s iPhoneSE
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)

// iPhone6 7 8
#define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? (CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) || CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size)) : NO)

// iPhone6plus  iPhone7plus iPhone8plus
#define iPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? (CGSizeEqualToSize(CGSizeMake(1125, 2001), [[UIScreen mainScreen] currentMode].size) || CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size)) : NO)

// iPhoneX
#define iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)

// 是否为苹果手机
#define iPhone (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)

// 6以下屏幕，不含6
#define UnderIphone6 (iPhone4S || iPhone5)

#pragma mark - UI尺寸

// 屏幕bounds
#define kScreenBounds UIScreen.mainScreen.bounds
// 屏幕size
#define kScreenSize UIScreen.mainScreen.bounds.size
// 屏宽
#define kScreenWidth kScreenSize.width
// 屏高
#define kScreenHeight kScreenSize.height

// 系统视图高度
#define kNavBarHeight (self.navigationController.navigationBar.frame.size.height)
#define kStatusBarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height)
#define kTopBarHeight (kNavBarHeight+kStatusBarHeight)
#define kTabbarHeight ([[UIApplication sharedApplication] statusBarFrame].size.height>20?83:49)

//缩放比例
#define kWidthRatio6 kScreenWidth/375.0
#define kHeightRatio6 kScreenHeight/667.0
#define kWidthRatio6P kScreenWidth/414.0
#define kHeightRatio6P kScreenHeight/736.0

// 相对于设计标准计算缩放数值
#define kScale6(x) kScreenWidth / 375.0 * (x)
#define kScale6P(x) kScreenWidth / 414.0 * (x)
#define kScaleSize6(w, h) CGSizeMake(kScale6(w), Scale6(h))
#define kScaleSize6P(w, h) CGSizeMake(kScale6P(w), kScale6P(h))

//字体缩放 reg标准 bold粗体
#define kFontReg6(x) [UIFont systemFontOfSize:kScale6(x)];
#define kFontReg6P(x) [UIFont systemFontOfSize:kScale6P(x)];
#define kFontBold6(x) [UIFont boldSystemFontOfSize:kScale6(x)];
#define kFontBold6P(x) [UIFont boldSystemFontOfSize:kScale6P(x)];

#pragma mark - 引用

#ifndef WeakSelf
#define WeakSelf __weak __typeof(self)weakSelf = self;
#endif

#ifndef StrongSelf
#define StrongSelf __strong __typeof(weakSelf)strongSelf = weakSelf;
#endif

// @weakify(self) @strongify(self)方式的宏定义
#ifndef weakify
    #if DEBUG
        #if __has_feature(objc_arc)
            #define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
        #else
            #define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
        #endif
    #else
        #if __has_feature(objc_arc)
            #define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
        #else
            #define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
        #endif
    #endif
#endif

#ifndef strongify
    #if DEBUG
        #if __has_feature(objc_arc)
            #define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
        #else
            #define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
        #endif
    #else
        #if __has_feature(objc_arc)
            #define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
        #else
            #define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
        #endif
    #endif
#endif


#endif /* PLMacro_h */
